package gases.as3.librarys.core.display
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.IEventDispatcher;
	import flash.geom.Rectangle;
	
	import gases.as3.librarys.core.events.MaskControllerEvent;
	
	public class GMaskContainer extends GDisplayContainer
	{
		public function GMaskContainer(target:DisplayObject,maskRect:Rectangle)
		{
			_container = target;
			_maskRect = maskRect;
		}
		
		override protected function initialize():void
		{
			super.initialize();
			this.addChild(_container);
			
			_maskSp = new Sprite();
			_maskSp.graphics.beginFill(0x000000,1);
			_maskSp.graphics.drawRect(_maskRect.x, _maskRect.y, _maskRect.width, _maskRect.height);
			_maskSp.graphics.endFill();
			this.mask = _maskSp;
			this.addChild(_maskSp);
			
			_controls = new Array();
		}
		
		/**
		 * 清理所有控制器 
		 * 
		 */		
		public function clearAllController():void
		{
			while(_controls.length > 0)
			{
				var ctl:IEventDispatcher = _controls.pop() as IEventDispatcher;
				unRegsiterController(ctl);
			}
		}
		
		public function moveMaskLeft(value:Number):void
		{
			if((_container.x + value) < _maskRect.x)
				_container.x += value;
			else
				_container.x = _maskRect.x;
		}
		
		public function moveMaskRight(value:Number):void
		{
			if((_container.x - value + _container.width) > _maskRect.x + _maskRect.width)
				_container.x -= value;
			else
				_container.x = _maskRect.x + _maskRect.width - _container.width;
		}
		
		public function moveMaskTop(value:Number):void
		{
			if((_container.y + value) < _maskRect.y)
				_container.y += value;
			else
				_container.y = _maskRect.y;
		}
		
		public function moveMaskBottom(value:Number):void
		{
			if((_container.y - value + _container.height) > _maskRect.y + _maskRect.height)
				_container.y -= value;
			else
				_container.y = _maskRect.y + _maskRect.height - _container.height;
		}
		
		public function moveToMaxBottom():void
		{
			_container.y = _maskRect.y + _maskRect.height - _container.height;
		}
		
		public function moveToMaxTop():void
		{
			_container.y = _maskRect.y;
		}
		
		public function moveToMaxLeft():void
		{
			_container.x = _maskRect.x;
		}
		
		public function moveToMaxRight():void
		{
			_container.x = _maskRect.x + _maskRect.width - _container.width;
		}
		
		public function setMaskYPosition(value:Number):void
		{
			if(value > _container.height)
				value = _container.height - _maskRect.height;
			_container.y = -value ;
		}
		
		public function setMaskXPosition(value:Number):void
		{
			if(value > _container.width)
				value = _container.width- _maskRect.width;
			_container.y = -value ;
		}
		
		public function regsiterController(control:IEventDispatcher):void
		{
			control.addEventListener(MaskControllerEvent.TO_LEFT,onMaskLeft);
			control.addEventListener(MaskControllerEvent.TO_RIGHT,onMaskRight);
			control.addEventListener(MaskControllerEvent.TO_TOP,onMaskTop);
			control.addEventListener(MaskControllerEvent.TO_BOTTOM,onMaskBottom);
			control.addEventListener(MaskControllerEvent.TO_MAX_BOTTOM,onToMaxBottom);
			control.addEventListener(MaskControllerEvent.TO_MAX_LEFT,onToMaxLeft);
			control.addEventListener(MaskControllerEvent.TO_MAX_RIGHT,onToMaxRight);
			control.addEventListener(MaskControllerEvent.TO_MAX_TOP,onToMaxTop);
			control.addEventListener(MaskControllerEvent.SET_Y_POSITION,onSetYPosition);
			control.addEventListener(MaskControllerEvent.SET_X_POSITION,onSetXPosition);
			
			_controls.push(control);
		}
		
		public function unRegsiterController(control:IEventDispatcher):void
		{
			control.removeEventListener(MaskControllerEvent.TO_LEFT,onMaskLeft);
			control.removeEventListener(MaskControllerEvent.TO_RIGHT,onMaskRight);
			control.removeEventListener(MaskControllerEvent.TO_TOP,onMaskTop);
			control.removeEventListener(MaskControllerEvent.TO_BOTTOM,onMaskBottom);
			control.removeEventListener(MaskControllerEvent.TO_MAX_BOTTOM,onToMaxBottom);
			control.removeEventListener(MaskControllerEvent.TO_MAX_LEFT,onToMaxLeft);
			control.removeEventListener(MaskControllerEvent.TO_MAX_RIGHT,onToMaxRight);
			control.removeEventListener(MaskControllerEvent.TO_MAX_TOP,onToMaxTop);
			control.removeEventListener(MaskControllerEvent.SET_Y_POSITION,onSetYPosition);
			control.removeEventListener(MaskControllerEvent.SET_X_POSITION,onSetXPosition);
			
			var index:int = _controls.indexOf(control);
			if(index != -1)
			{
				_controls.splice(index, 1);
			}
		}
		
		private function onMaskLeft(e:MaskControllerEvent):void
		{
			moveMaskLeft(e.value);
		}
		
		private function onMaskRight(e:MaskControllerEvent):void
		{
			moveMaskRight(e.value);
		}
		
		private function onMaskTop(e:MaskControllerEvent):void
		{
			moveMaskTop(e.value);
		}
		
		private function onMaskBottom(e:MaskControllerEvent):void
		{
			moveMaskBottom(e.value);
		}
		
		private function onToMaxBottom(e:MaskControllerEvent):void
		{
			moveToMaxBottom();
		}
		
		private function onToMaxTop(e:MaskControllerEvent):void
		{
			moveToMaxTop();
		}
		
		private function onToMaxLeft(e:MaskControllerEvent):void
		{
			moveToMaxLeft();
		}
		
		private function onToMaxRight(e:MaskControllerEvent):void
		{
			moveToMaxRight();
		}
		
		private function onSetYPosition(e:MaskControllerEvent):void
		{
			setMaskYPosition(e.value);
		}
		
		private function onSetXPosition(e:MaskControllerEvent):void
		{
			setMaskXPosition(e.value);
		}
		
		public function dispose():void
		{
			clearAllController();
			this.removeChild(_container);
			this.removeChild(_maskSp);
			_maskSp.graphics.clear();
			_maskSp = null;
			_maskRect = null;
			_container = null;
		}
		
		private var _container:DisplayObject;
		private var _maskSp:Sprite;
		private var _maskRect:Rectangle;
		private var _controls:Array;
	}
}

